package heap;

import java.util.HashMap;
import java.util.Map;

/**
 * 138. 复制带随机指针的链表
 * https://leetcode-cn.com/problems/copy-list-with-random-pointer/
 */
public class 复制带随机指针的链表 {
    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
    public Node copyRandomList(Node head) {
        if (head == null){
            return null;
        }
        Map<Node , Node>  map = new HashMap<>();
        for (Node x = head; x != null ; x = x.next) {
            Node y = new Node(x.val);
            map.put(x , y);
        }
        for (Node x = head; x != null ; x = x.next) {
            map.get(x).next = map.get(x.next);
            map.get(x).random = map.get(x.random);
        }
        return map.get(head);
    }

}
